<?php

class Email
{
	private $error;
	public $cookie_jar1;
	public function __construct()
	{}
	
	public function __destruct()
	{
		$this->clean();
	}

	public function get_contacts($account, $password)
	{
		$account = current(explode('@', $account));
		$account = strtolower(trim($account));
		$password = trim($password);
		if (empty($account) || empty($password)) {
			$this->error = 'err_01';
			return false;
		}

		$tmp_dir = PATH_SEPARATOR == ':' ? '/dev/shm/invite/' : 'c:/windows/temp/';
		$tmp_dir = $tmp_dir . substr(md5(microtime(true) . rand(0, 999999999)), 0, 2);

		$cookie_jar1 = tempnam($tmp_dir, 'cookie1');
		$this->cookie_jar1 = $cookie_jar1;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_TIMEOUT, 3);
		curl_setopt($ch, CURLOPT_URL, 'http://reg.163.com/login.jsp?type=1&product=mail126&url=http://entry.mail.126.com/cgi/ntesdoor?hid%3D10010102%26lightweight%3D1%26language%3D0%26style%3D-1');
		curl_setopt($ch, CURLOPT_POST, 1);
		$request = 'verifycookie=1&username='.$user.'&password='.$password.'&selType=jy&%B5%C7%C2%BC%D3%CA%CF%E4=%B5%C7%C2%BC%D3%CA%CF%E4&style=16&product=mail163';
		$request = 'domain=126.com&language=0&bCookie=&username=' .$account. '@126.com&savelogin=&user='. $account .'&password=' .$password. '&style=-1&enter.x=%B5%C7%A1%A1%C2%BC';
		curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_NOBODY, false);
		$html=curl_exec($ch);
		curl_close($ch);
		
		preg_match_all('/window\.location\.replace\("(.*?)"\);/', $html, $match);
		
		if(strlen($match[1][0]) == 0)
		{
			return false;
		}
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_TIMEOUT, 3);
		curl_setopt($ch, CURLOPT_URL, $match[1][0]);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar1);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar1);
		$html=curl_exec($ch);
		curl_close($ch);
		
		preg_match_all('/window\.location\.replace\("(.*?)"\);/', $html, $match);
		
		if(strlen($match[1][0]) == 0)
		{
			return false;
		}
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_TIMEOUT, 3);
		curl_setopt($ch, CURLOPT_URL, $match[1][0]);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar1);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar1);
		$html=curl_exec($ch);
		curl_close($ch);
		
		preg_match_all('/<p>The document has moved <a href="(.*?)">here<\/a>\.<\/p>/', $html, $m1);
		
		if(strlen($m1[1][0]) == 0)
		{
			return 'err_01';
		}
		$find1   = 'sid=';
		$find2   = '">here';
		$pos1 = strpos($html, $find1);
		$pos2 = strpos($html, $find2);
		$l=$pos2-$pos1-4;
		$sess=substr($html, $pos1+4,$l);
		$sess=trim($sess);
		
		if (strlen($sess) > 32 || strlen($sess) < 6) {
			return 'err_01';
		}
		
		$list = parse_url($m1[1][0]);
		
		if(strlen($list['scheme']) == 0 || strlen($list['host']) == 0)
		{
			return 'error_01';
		}

		$post_url = 'http://' . $list['host'] . '/a/s?sid=' . $sess . '&func=global:sequential';
		$post_data = '<?xml version="1.0"?><object><array name="items"><object><string name="func">pab:searchContacts</string><object name="var"><array name="order"><object><string name="field">FN</string><boolean name="ignoreCase">true</boolean></object></array></object></object></array></object>';
		$http_headers = array('Content-Type: application/xml');

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $post_url);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_NOBODY, false);
		curl_setopt($ch, CURLOPT_TIMEOUT, 3);
		$return = curl_exec($ch);
		curl_close($ch);

		$dom = new DOMDocument('1.0', 'UTF-8');
		if (!$dom->loadXML($return)) {
			$this->error = 'err_02';
			return false;
		}
		//exit($dom->saveXML());
		$xpath = new DOMXPath($dom);
		$objects = $xpath->query('//result/array/object/array/object');
		$contacts = array();
		foreach ($objects as $obj) {
			$tmp = array();
			foreach ($obj->childNodes as $node) {
				if (is_object($node->attributes)) {
					$attr = $node->attributes->getNamedItem('name')->value;
					if ($attr == 'EMAIL;PREF') {
						$tmp['email'] = $node->nodeValue;
					} else if ($attr == 'FN') {
						$tmp['name'] = $node->nodeValue;
					}
				}
			}
			array_push($contacts, $tmp);
		}
		if (empty($contacts)) {
			$this->error = 'err_02';
			return false;
		}
		foreach ($contacts as $k => $v) {
			foreach ($v as $sk => $sv) {
				$v[$sk] = iconv('UTF-8', 'GBK', $sv);
			}
			$contacts[$k] = $v;
		}
		
		return $contacts;
	}

	public function get_error()
	{
		return $this->error;
	}
	
	public function clean()
	{
		if(file_exists($this->cookie_jar1))
		{
			@unlink($this->cookie_jar1);
		}
	}
}

?>